package com.zzsy.carosshopprojects.activitys.home.mainpage.common.scan;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.ResultBean;
import com.zzsy.carosshopprojects.bean.TokenBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;

import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class ScanOnlyMode extends BaseViewMode {
    private MutableLiveData<TokenBean> live;
    private MutableLiveData<ResultBean> liveResult;

    public MutableLiveData<TokenBean> getLive() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }

    public MutableLiveData<ResultBean> getLiveResult() {
        if (liveResult == null)
            liveResult = new MutableLiveData<>();
        return liveResult;
    }

    /**
     * 获取Token
     *
     * @param context
     */
    public void getToken(Context context,String result) {
        RequestUtils.getToken(context, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String tokenMsg, int code) {
                if (code == ConfigureFile.code) {
                    TokenBean bean = new TokenBean();
                    bean.setToken(tokenMsg);
                    bean.setResult(result);
                    live.postValue(bean);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), tokenMsg);
                }

            }
        });
    }

    /**
     * 加油订单签名
     *
     * @param context
     * @param map
     */
    public void OrderPay(Context context, Map<String, String> map,String token) {
        ResultBean resultBean = new ResultBean();
        RequestUtils.OrderPay(context, map,token, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {
                resultBean.setCode(ConfigureFile.code);
                liveResult.postValue(resultBean);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                resultBean.setCode(code);
                resultBean.setMessage(errorMsg);
                liveResult.postValue(resultBean);
            }
        });
    }
}
